//预约信息
$(function(){
  var $s1 = $("#clinic_Id") ;
  var $s2 = $("#doctor_Id") ;
  var $s3 = $("#register_DateTime") ;
  var s2 ;
  var s3 ;
  //门诊
  $s1.change(function(){
        s2 = "" ;
        s3 = "" ;
        $s2.html("");
		$s3.html("");
		if(this.selectedIndex==-1) return;
		var s1_curr_val = this.options[this.selectedIndex].value;
		$.ajax({
			type: "POST",
			url: "findDoctors_By_Clinic_Id.do?clinic_Id="+s1_curr_val,
			dataType: 'text',
			async: false,
			error:function(data){
				
			},
			success: function(data){
			      var ops = data.split("|");
			      appendOptionsTo($s2,"","") ; 
                  for(var i = 0; i < ops.length-1; i++){
                      
                      var op = ops[i]; 
                      var ss = op.split(",");
                      appendOptionsTo($s2,ss[1],ss[0]) ;
                      
                     
                  }										
			}
			
		});	
		
		$.ajax({
			type: "POST",
			url: "workDateTime_By_Clinic_Id.do?clinic_Id="+s1_curr_val,
			dataType: 'text',
			async: false,
			error:function(data){
				
			},
			success: function(data){
			      var ops = data.split("|") ;
			      appendOptionsTo($s3,"","") ;
                  for(var i = 0; i < ops.length-1; i++){ 
                      var op = ops[i]; 
                      var ss = op.split(",");
                      appendOptionsTo($s3,ss[0]+" "+ss[1],ss[0]+","+ss[1]) ;
                     // if(ss[1] != ""){
                      //  appendOptionsTo($s4,ss[1],ss[1]) ;
                      //}
                     
                  }										
			}
			
		});	
			  					
  });
  
  
  //医生
   $s2.change(function(){
            if(s3 == ""){//能根据时间和医生交互查找
				$s3.html("");
				var s1_curr_val = $s1[0].options[$s1[0].selectedIndex].value;
				if(this.selectedIndex==-1) return;
				var s2_curr_val = this.options[this.selectedIndex].value;
				$.ajax({
					type: "POST",
					url: "workDateTime_By_Clinic_IdAndDoctor_Id.do?clinic_Id="+s1_curr_val+"&doctor_Id="+s2_curr_val,
					
					dataType: 'text',
					async: false,
					error:function(data){
						
					},
					success: function(data){
					      var ops = data.split("|"); 
					      appendOptionsTo($s3,"","") ;
					      
		                  for(var i = 0; i < ops.length-1; i++){
		                      
		                      var op = ops[i]; 
		                      var ss = op.split(",");
		                      appendOptionsTo($s3,ss[0]+" "+ss[1] ,ss[0]+","+ss[1]) ;
		                      //appendOptionsTo($s4,ss[1],ss[1]) ;
		                  }										
					}
					
				});
				
				s2 = s2_curr_val ;
		  }	
  }); 
  //时间
   $s3.change(function(){  
             if(s2 == ""){//能根据时间和医生交互查找
				$s2.html("");
				var s1_curr_val = $s1[0].options[$s1[0].selectedIndex].value;
				if(this.selectedIndex==-1) return;
				var s3_curr_val = this.options[this.selectedIndex].value;
				var ss3 = s3_curr_val.split(",");
				$.ajax({
					type: "POST",
					url: "doctors_By_Clinnic_IdAndDateTime.do?workDate="+ss3[0]+"&workTime="+ss3[1],
					dataType: 'text',
					async: false,
					error:function(data){
						
					},
					success: function(data){
					      var ops = data.split("|"); 
					      appendOptionsTo($s2,"","") ;
		                  for(var i = 0; i < ops.length-1; i++){
		                      var op = ops[i]; 
		                      var ss = op.split(",");
		                      appendOptionsTo($s2,ss[1],ss[0]) ;
		                      //appendOptionsTo($s4,ss[1],ss[1]) ;
		                  }										
					}
					
				});	
				
				s3 = s3_curr_val ;	
				
		 }			
  });
  
  function appendOptionsTo($o,text,value){
      var $opt = $("<option>") ;
      $opt.html(text) ;
      $opt.attr("value",value) ;
      $opt.appendTo($o);
  } 
});